package ru.java2D;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.Timer;


/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 13.04.13
 * Time: 21:47
 * To change this template use File | Settings | File Templates.
 */
public class Road extends JPanel implements ActionListener, Runnable{

    Timer mainTimer = new Timer(20, this);

    Image img = new ImageIcon(getClass().getClassLoader().getResource("res/road.jpg")).getImage();

    Player p = new Player();

    Thread enemiesFactory = new Thread(this);

    Thread audio = new Thread(new Audio());

    List<Enemy> enemies = new ArrayList<Enemy>();

    public Road() {
        mainTimer.start();
        enemiesFactory.start();
        audio.start();
        addKeyListener(new MyKeyAdapter());
        setFocusable(true);
    }

    @Override
    public void run() {
        while (true){
            Random rand = new Random();
            try {
                Thread.sleep(rand.nextInt(5000));
                enemies.add(new Enemy(1200,
                        rand.nextInt(370),
                        rand.nextInt(30),
                        this));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    private class MyKeyAdapter extends KeyAdapter{

        public void keyPressed(KeyEvent e){
            p.keyPressed(e);
        }
        public void keyReleased(KeyEvent e){
            p.keyReleased(e);
        }
    }

    public void paint(Graphics g){

        g.drawImage(img, p.layer1, 0, null);
        g.drawImage(img, p.layer2, 0, null);

        g.drawImage(p.img, p.x, p.y, null);

        double v = (80/Player.MAX_V) * p.v;

        g.setColor(Color.WHITE);
        Font font = new Font("Arial", Font.ITALIC, 20);
        g.setFont(font);
        g.drawString("Скорость: " + v + "км/ч", 30, 30);

        Iterator<Enemy> i = enemies.iterator();
        while (i.hasNext()){
            Enemy e = i.next();
            if (e.x >= 2400 || e.x <= -2400) {
                i.remove();
            }
            else {
                e.move();
                g.drawImage(e.img, e.x, e.y, null);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        p.move();
        repaint();
        crash();
        testWin();
    }

    private void testWin() {
        if (p.s > 100000){
            JOptionPane.showMessageDialog(null, "Победа! Путь пройден.");
            System.exit(0);
        }
    }

    public void crash(){
        Iterator<Enemy> i = enemies.iterator();
        while (i.hasNext()){
            Enemy e = i.next();
            if (p.getRect().intersects(e.getRect())){
                JOptionPane.showMessageDialog(null, "Танк уничтожен :)");
                System.exit(1);
            }
        }
    }
}
